package com.schoolET.service.admin.adminImpl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.schoolET.config.OssConfig;
import com.schoolET.entity.Region;
import com.schoolET.entity.dto.RegionInfoDTO;
import com.schoolET.entity.vo.RegionVO;
import com.schoolET.error.costom.AddRegionException;
import com.schoolET.error.costom.NoPhotoException;
import com.schoolET.error.costom.NoRegionException;
import com.schoolET.mapper.RegionMapper;
import com.schoolET.service.admin.IRegionService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * <p>
 * 区域信息 服务实现类
 * </p>
 *
 * @author le
 * @since 2024-05-31
 */
@Service
public class RegionServiceImpl extends ServiceImpl<RegionMapper, Region> implements IRegionService {

    @Resource
    private OssConfig ossConfig;

    @Override
    public Page<Region> getManaList(Integer now, Integer num, String position) {
        QueryWrapper<Region> queryWrapper = new QueryWrapper<>();
        if (position != null && !"".equals(position)){
            queryWrapper.like("position",position);
        }
        Page<Region> regionPage = baseMapper.selectPage(
                new Page<>(now, num), queryWrapper);
        return regionPage;
    }

    @Override
    @Transactional
    public boolean addRegion(RegionVO regionVO, MultipartFile file) throws IOException {
        if (file == null) {
            throw new NoPhotoException();
        }
        Region region = new Region();
        //生成一个6位数的随机数
        BeanUtil.copyProperties(regionVO,region);
        Integer code = (int) (Math.random() * (9 + 1) * 100000);
        QueryWrapper<Region> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("invitation_code", code);
        Region region1 = baseMapper.selectOne(queryWrapper);
        Region region2 = baseMapper.getRegion(regionVO.getPosition());
        if (region1 != null || region2 != null) {
            throw new AddRegionException();
        }
        region.setInvitationCode(code);
        String path = ossConfig.putObject(file, "region");
        region.setPhoto(path);
        save(region);
        return true;
    }

    @Override
    public boolean editRegion(RegionVO regionVO, Integer id, MultipartFile file) throws IOException {
        if (file == null) {
            throw new NoPhotoException();
        }
        Region byId = getById(id);
        if (byId == null) {
            throw new NoRegionException();
        }
        String photo = byId.getPhoto();
        Region region = new Region();
        BeanUtil.copyProperties(regionVO, region);
        String path = ossConfig.putObject(file, "region");
        region.setId(id);
        region.setPhoto(path);
        ossConfig.getOSSClient().deleteObject(ossConfig.getBucketName(), photo);
        saveOrUpdate(region);
        return true;
    }

    @Override
    public RegionInfoDTO getRegionInfoById(Integer id) {
        Region region = baseMapper.selectById(id);
        if (region == null) {
            throw new NoRegionException();
        }
        RegionInfoDTO regionInfoDTO = new RegionInfoDTO();
        BeanUtil.copyProperties(region, regionInfoDTO);
        return regionInfoDTO;
    }

    @Override
    public Region selectRegionByCode(Integer code) {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("invitation_code", code);
        Region region = baseMapper.selectOne(queryWrapper);
        if (region == null){
            throw new NoRegionException();
        }
        return region;
    }
}
